//
//  LXMainTabBarController.m
//  lingXiu
//
//  Created by 李田 on 17/3/16.
//  Copyright © 2017年 tianyao. All rights reserved.
//

#import "LXMainTabBarController.h"
#import "LXBaseNavigationController.h"

@interface LXMainTabBarController ()

@end

@implementation LXMainTabBarController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    NSDictionary *homeDict = @{@"name" :@"LXHomeViewController", @"title" :@"首页", @"imageName": @"v2_home"};
    NSDictionary *orderDict = @{@"name" :@"LXOrderViewController", @"title" :@"闪送超市", @"imageName": @"v2_order"};
    NSDictionary *cartDict = @{@"name" :@"LXCartViewController", @"title" :@"购物车", @"imageName": @"shopCart"};
    NSDictionary *mineDict = @{@"name" :@"LXMineViewController", @"title" :@"我的", @"imageName": @"v2_my"};
    
    UIViewController *homeVc = [self createViewControllerWithDict:homeDict];
    UIViewController *orderVc = [self createViewControllerWithDict:orderDict];
    UIViewController *cartVc = [self createViewControllerWithDict:cartDict];
    UIViewController *mineVc = [self createViewControllerWithDict:mineDict];
    
    self.viewControllers = @[homeVc, orderVc, cartVc, mineVc];
    
    [self addAnimation];
}

- (UIViewController *)createViewControllerWithDict: (NSDictionary *)dict {
    
    UIViewController *vc = [[NSClassFromString(dict[@"name"]) alloc] init];
    // 设置文字
    [vc.tabBarItem setTitle:dict[@"title"]];
    // 设置图片
    [vc.tabBarItem setImage:[[UIImage imageNamed:dict[@"imageName"]] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]];
    [vc.tabBarItem setSelectedImage:[[UIImage imageNamed:[NSString stringWithFormat:@"%@_selected",dict[@"imageName"]]] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]];
    
    return [[LXBaseNavigationController alloc] initWithRootViewController:vc];
}

//    MARK:tabBarButton添加点击事件
- (void)addAnimation {
    // 遍历子tabBar的子控件
    for (UIControl *tabBarButton in self.tabBar.subviews) {
        if ([tabBarButton isKindOfClass:NSClassFromString(@"UITabBarButton")]) {
            [tabBarButton addTarget:self action:@selector(tabBarButtonClick:) forControlEvents:UIControlEventTouchUpInside];
        }
    }
}

//    MARK: button点击事件
- (void)tabBarButtonClick :(UIControl *)tabBarButton {
    
    for (UIView *imageView in tabBarButton.subviews) {
        if ([imageView isKindOfClass:NSClassFromString(@"UITabBarSwappableImageView")]) {
            //实现帧动画,需求自定义
            CAKeyframeAnimation *animation = [CAKeyframeAnimation animation];
            animation.keyPath = @"transform.scale";
            animation.values = @[@1.0,@1.3,@0.9,@1.15,@0.95,@1.02,@1.0];
            animation.duration = 0.5;
            animation.calculationMode = kCAAnimationCubic;
            //动画添加
            [imageView.layer addAnimation:animation forKey:nil];
        }
    }
}

@end











